<template>
	<view class="container">

		<view class="cd-bg">
			<!-- 基本信息 -->
			<view class="cd-title">
				<uni-row>
					<uni-col :span="20">
						<view class="param">基本信息</view>
					</uni-col>
					<uni-col :span="4">
						<view class="param" v-if="detail.sta==0&&detail.chexiao==0"><uni-tag text="审核" type="warning"></uni-tag></view>
						<view class="param" v-if="detail.sta==1&&detail.chexiao==0"><uni-tag text="通过" type="primary"></uni-tag></view>
						<view class="param" v-if="detail.sta==2&&detail.chexiao==0"><uni-tag text="拒绝" type="error"></uni-tag></view>
						<view class="param" v-if="detail.chexiao==1"><uni-tag text="撤销" type="default"></uni-tag></view>
					</uni-col>
				</uni-row>
			</view>
			<view class="cd-body">
				<view class="param"><b>申请人：</b>{{detail.shenqingname}}</view>
				<view class="param"><b>申请时间：</b>{{getFormatedTimeStr(detail.createTime)}}</view>
			</view>

			<!-- 特殊参数 -->
			<view class="cd-line">
				<view class="param">申请信息</view>
			</view>
			<view class="cd-body">
				<view class="param"><b>编号：</b>{{detail.bianhao}}</view>
				<view class="param"><b>来文单位：</b>{{detail.danwei}}</view>
				<view class="param"><b>文号：</b>{{detail.wenhao}}</view>
				<view class="param"><b>标题：</b>{{detail.biaoti}}</view>
				<view class="param"><b>公文类型：</b>{{detail.fileTag}}</view>
				<view class="param"><b>来文时间：</b>{{getFormatedTimeStr( detail.chengwenDate,"YYYY-MM-DD")}}</view>
				<view class="param"><b>内容摘要：</b>{{detail.zhaiyao}}</view>
			</view>

			<!-- 相关附件 -->
			<view class="cd-line">
				<view class="param"><b>相关附件</b></view>
			</view>
			<view class="cd-body">

				<!-- 附件 -->
				<view class="param"><b>附件：</b></view>
				<view v-for="(it,idx) in detail.attaches" :key="idx">
				<text style="font-size: 16px;color: #51bb88;" @click="clickAttach(it)">{{ it.name }}</text>
				<br>
				<uni-link style="color:purple;font-size: 15px;" :href="escapePath(it.path)" text="下载"></uni-link>
				</view>

			</view>

			<!-- 审核进度 -->
			<view class="cd-line">
				<view class="param"><b>审核进度</b></view>
			</view>

			<!-- {{ detail.auditors }} -->
			<view class="cd-body">
				<view v-for="(audi, idx, id) in detail.auditors" :key="idx">
					<view v-if="idx != 'auditor2'">
						<uni-row>
							<uni-col :span="24">
								<view v-if="id>0" class="step-arrow">
									<uni-icons type="bottom" size="30" color='#a5eaff'></uni-icons>
								</view>
							</uni-col>
						</uni-row>
						<view class="step">
							<view class="step-title">
								<uni-row>
									<uni-col :span="8">
										<uni-row>
											<uni-col :span="12">
												<view class="step-title-left">{{audi.name}}</view>
											</uni-col>
											<uni-col :span="12">
												<view v-if="cuibanShow(id)" style="text-align: right; color: #0b847b;" @click="cuiban(audi.uid)">
														<uni-icons type="paperplane-filled" color="#0b847b" size="16"></uni-icons>催办
												</view>
											</uni-col>
										</uni-row>
									</uni-col>
									<uni-col :span="8">
										<view class="step-title-center">
											<uni-tag text="未审核" size="mini" type="default" v-if="audi.sta==0"></uni-tag>
											<uni-tag text="同意" size="mini" type="primary" v-if="audi.sta==1"></uni-tag>
											<uni-tag text="拒绝" size="mini" type="error" v-if="audi.sta==2"></uni-tag>
										</view>
									</uni-col>
									<uni-col :span="8">
										<view class="step-title-right">{{audi.stepName}}</view>
									</uni-col>
								</uni-row>
							</view>
							<view class="step-info">
								<uni-row>
									<uni-col :span="16" v-if="audi.sta>0 && audi.msg=='' ">
										<view class="msg"><b>意见：</b>无</view>
									</uni-col>
									<uni-col :span="16" v-if="audi.sta>0 && audi.msg!='' ">
										<view class="msg"><b>意见：</b>{{audi.msg}}</view>
									</uni-col>
									<uni-col :span="8" v-if="audi.sta>0">
										<view class="img-qm">
											<image :src="audi.signimg" />
										</view>
									</uni-col>
								</uni-row>
							</view>
							<view class="step-footer">
								<uni-row>
									<uni-col :span="24" v-if="audi.sta>0">
										<view class="step-footer-right">{{getFormatedTimeStr(audi.ts)}}</view>
									</uni-col>
								</uni-row>
							</view>
						</view>
					</view>
				</view>
			</view>


			<!-- 承办进度 -->
			<view class="cd-line" v-if="detail.chengbanSta>0">
				<view class="param"><b>批办进度</b></view>
			</view>

			<view class="cd-body" v-if="detail.chengbanSta>0">
				<view v-for="(cbr, idx,id) in detail.chengbanren" :key="idx">
					<uni-row v-if="cbr.selectDepart != 1">
						<uni-col :span="24">
							<view v-if="id>0" class="step-arrow">
								<uni-icons type="bottom" size="30" color='#a5eaff'></uni-icons>
							</view>
						</uni-col>
					</uni-row>
					<view class="step" v-if="cbr.selectDepart != 1">
						<view class="step-title-warn">
							<uni-row>
								<uni-col :span="8">
									<uni-row>
										<uni-col :span="12">
											<view class="step-title-left">{{cbr.name}}</view>
										</uni-col>
										<uni-col :span="12">
											<view v-if="cbr.sta==0" style="text-align: right; color: #0b847b;" @click="cuiban(cbr.uid, '待完成')">
												<uni-icons type="paperplane-filled" color="#0b847b" size="16"></uni-icons>催办
											</view>
										</uni-col>
									</uni-row>
									<!-- <view class="step-title-left">{{cbr.name}}</view> -->
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-center">
										<uni-tag text="未完成" size="mini" type="default" v-if="cbr.sta==0"></uni-tag>
										<uni-tag text="完成" size="mini" type="primary" v-if="cbr.sta==1"></uni-tag>
									</view>
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-right">{{cbr.positionName}}</view>
								</uni-col>
							</uni-row>
						</view>
						<view class="step-info">
							<uni-row>
								<uni-col :span="16" v-if="cbr.sta>0 && cbr.msg=='' ">
									<view class="msg"><b>意见：</b>无</view>
								</uni-col>
								<uni-col :span="16" v-if="cbr.sta>0 && cbr.msg!='' ">
									<view class="msg"><b>意见：</b>{{cbr.msg}}</view>
								</uni-col>
								<uni-col :span="8" v-if="cbr.sta>0">
									<view class="img-qm">
										<image :src="cbr.signimg" />
									</view>
								</uni-col>
							</uni-row>
						</view>
						<view class="step-footer">
							<uni-row>
								<uni-col :span="24" v-if="cbr.sta>0">
									<view class="step-footer-right">{{getFormatedTimeStr(cbr.ts)}}</view>
								</uni-col>
							</uni-row>
						</view>
					</view>
				</view>
			</view>

			<view class="cd-line" v-if="detail.chengbanSta>0">
				<view class="param"><b>具体经办人</b></view>
			</view>
			<view class="cd-body" v-if="detail.chengbanSta>0">
				<view v-for="(cbr, idx) in detail.readers" :key="idx">
					<view class="step">
						<view class="step-title-warn">
							<uni-row>
								<uni-col :span="8">
									<uni-row>
										<uni-col :span="12">
											<view class="step-title-left">{{cbr.name}}</view>
										</uni-col>
										<uni-col :span="12">
											<view style="text-align: right; color: #0b847b;" @click="cuiban(cbr.uid, '待完成')">
												<uni-icons type="paperplane-filled" color="#0b847b" size="16"></uni-icons>催办
											</view>
										</uni-col>
									</uni-row>
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-center">
										<uni-tag text="已阅办" size="mini" type="primary" v-if="cbr.sta==1"></uni-tag>
										<uni-tag text="未阅办" size="mini" type="default" v-else></uni-tag>
									</view>
								</uni-col>
								<uni-col :span="8">
									<view class="step-title-right">{{cbr.positionName}}</view>
								</uni-col>
							</uni-row>
						</view>
						<!-- <view class="step-info">
							<uni-row>
								<uni-col :span="24" v-if="cbr.sta>0">
									<view class="msg"><b>分发人：{{ getReaderNames(detail.readers) }}</b>无</view>
								</uni-col>
							</uni-row>
						</view> -->
						<view class="step-footer">
							<uni-row>
								<uni-col :span="24" v-if="cbr.sta>0">
									<view class="step-footer-right">{{getFormatedTimeStr(cbr.ts)}}</view>
								</uni-col>
							</uni-row>
						</view>
					</view>
				</view>
			</view>

			<!-- 操作按钮 -->
			<view style="padding: 30px;"></view>

			<view class="cd-footer" v-if="see == 1 && isMyAgree() == 1 && userInfo.uid == 10 && detail.sta == 0 ">
				<button @click="clickFinish" type="primary">{{ "结束流程" }}</button>
			</view>

			<view class="cd-footer" v-if="see == 1 && isMyAgree() == 1 && detail.sta == 0">
				<button @click="clickAudit(0)" type="primary">{{ userInfo.uid == 10 ? "转发": "审批" }}</button>
			</view>

			<view class="cd-footer" v-if="see == 1 && isMyAgreeChengban() == 1">
				<button @click="clickAudit(1)" type="warn">承办</button>
			</view>

			<view class="cd-footer" v-if="detail.chexiao == 0 && detail.sta == 0 && (detail.uid == userInfo.uid || userInfo.uid == 4)">
				<button @click="revert()" type="warn">撤销</button>
			</view>
<!-- v-if="detail.sta == 1" -->
			<view class="cd-footer" >
				<button @click="clickShoucang" type="warn">{{shoucangBtnStr()}}</button>
			</view>

		</view>

	</view>
</template>

<script>
	import {
		websock
	} from '../../net/websock'
	import moment from 'moment'
import contactSelectVue from '../common/contactSelect.vue'
	export default {
		data() {
			return {
				id: 0,
				taskid: 0,
				see: 0,
				detail: {},
				userInfo: {},
			}
		},
		async onReady() {
			try {
				this.userInfo = await websock.getUserInfo()
			} catch (e) {
				uni.showToast({
					title: e.message
				})
			}
		},
		async onLoad(params) {
			this.id = params.id
			this.taskid = params.taskid
			this.see = params.see || 0
			console.log('onload', params)

			this.getInfo();

			// this.getFileType();
		},
		methods: {
			getReaderNames(readers) {
				if (!readers || !Array.isArray(readers) || Array.length == 0) return ''
				let names = ''
				for (let i = 0; i != readers.length; i++) {
					names += readers[i].name
					if (i != readers.length - 1) {
						names += '、'
					}
				}
				return names
			},
			// 获取文件标签列表
			 async getFileType(tag) {
				let data = await  websock.fileTagList();
				for(let i=0;i<data.length;i++) {
					if(data[i].id === tag) {
						console.log(data[i].name)
						return data[i].name;
					}
				}
			},

			async getInfo() {
				this.detail = await websock.gongwenLiuzhuanDetail({
					id: this.id
				})

			// 获取登录人信息和经办人数据，判断当前登陆人是否是被选的具体经办人
			if (this.detail.readers && this.detail.readers.length) {
				const readers = this.detail.readers;
				const loginUserId = JSON.parse(uni.getStorageSync('loginUserInfo')).uid;
				const readerToRead = readers.find(reader => reader.sta !== 1 && reader.uid === loginUserId);

				if (readerToRead) {
					try {
						console.log(this.detail.id, 'detaildetail');
						const res = await websock.gongwenliuzhuanYueban({ id: this.detail.id });
						console.log(res);
						uni.showToast({ title: '阅办成功', icon: 'success' });

						// 更新具体经办人数据
						const data = await websock.gongwenLiuzhuanDetail({ id: this.id });
						this.detail.readers = data.readers;
					} catch (error) {
						// 异常处理
					}
				}

				console.log(loginUserId, 'loginUserId');
				console.log(this.detail.readers, 'loginUserId');
			}


				console.log(this.getFileType(this.detail.fileTag))
				this.getFileType(this.detail.fileTag).then((res) => {
					this.detail.fileTag = res;
				})

				websock.getUserInfoByUid({
					uid: this.detail.uid
				}).then(res => {
					this.detail.shenqingname = res.name
				})

				for (let key in this.detail.auditors) {
					websock.getUserInfoByUid({
						uid: this.detail.auditors[key].uid
					}).then(res => {
						this.detail.auditors[key].name = res.name
						this.detail.auditors[key].signimg = res.signimg
					})
				}

				for (let key in this.detail.chengbanren) {
					websock.getUserInfoByUid({
						uid: this.detail.chengbanren[key].uid
					}).then(res => {
						this.detail.chengbanren[key].name = res.name
						this.detail.chengbanren[key].signimg = res.signimg
					})
				}

			},

			async clickShoucang() {
				try {
					if (this.detail.shoucang) {
						await websock.cancelShoucang({
							id: this.detail.id,
							typ: 18
						})
						this.detail.shoucang = 0
					} else {
						await websock.shoucangGongwen({
							id: this.detail.id,
							typ: 18,
							title: this.detail.biaoti,
							content: this.detail.zhaiyao
						})
						this.detail.shoucang = 1
					}
				} catch (err) {
					console.error(err)
					uni.showToast({
						title: err.message || 'error'
					})
				}
			},
			// 进行审批
		clickAudit(typ) {
			let loginUser = JSON.parse(uni.getStorageSync('loginUserInfo')).userName;
			let user = false;
			if (loginUser === "陈皓玉") {
				user = true;
			}

			console.log(loginUser)
			// 0 -- 转发、审批 1 -- 承办
			// let url = `/pages/common/audit?taskid=${this.taskid}&typ=${typ}`;
			let url;
			if (typ === 1) {
				// 登录人是否是陈浩玉
				if(user) {
					url = `/pages/common/audit5?taskid=${this.taskid}&typ=${typ}&type=gongwenliuzhuan&user=${user}`
				} else {
					url = `/pages/common/audit?taskid=${this.taskid}&typ=${typ}&type=gongwenliuzhuan`
				}
			} else {
				url = `/pages/common/audit?taskid=${this.taskid}&typ=${typ}`
			}
			uni.navigateTo({
				url
			})
		},
			// 结束流程
			clickFinish() {
				uni.showModal({
					title: "提示",
					content: "是否结束流程？",
					success: async (res) => {
						if (res.confirm) {
							websock.finishGongwenLiuzhuan({id: this.id}).then(res => {
							uni.showToast({ title: '结束成功' })
							uni.$emit('agree_task', this.taskid)
							uni.navigateBack()
						})
						}
					}
				});
			},
			// 判断我是不是审批过
			isMyAgree() {
				let myid = this.userInfo.uid
				for (let key in this.detail.auditors) {
					if (this.detail.auditors[key].uid == myid && this.detail.auditors[key].sta == 0) {
						return 1
					}
				}
				return 0
			},
			// 判断我是不是承办过
			isMyAgreeChengban() {
				let myid = this.userInfo.uid
				console.log(this.detail.chengbanren)

				for (let key in this.detail.chengbanren) {
					if (this.detail.chengbanren[key].uid == myid && this.detail.chengbanren[key].sta == 0) {
						return 1
					}
				}
				return 0
			},
			// 获取格式化时间字符串
			getFormatedTimeStr(stamp, str = 'MM-DD HH:mm:ss') {
				return moment(stamp).format(str)
			},

			//点击放大U图片
			showBigImg(imgurl, cur) {
				uni.previewImage({
					current: cur,
					loop: true,
					urls: imgurl,
				})
			},
			shoucangBtnStr() {
				if (this.detail.shoucang) return '取消收藏'
				return '收藏'
			},
			async revert() {
				try {
					await websock.revertGongwenLiuzhuan({id: this.detail.id})
					uni.$emit('agree_task', this.detail.id)
					this.getInfo()
				} catch (err) {
					console.error(err)
				}
			},
			async clickAttach(item) {
				let last4 = item.path.substring(item.path.length - 4)
				if (last4 == '.pdf') {
					console.log('pdf')
					uni.navigateTo({ url: `/pages/pdf-view?url=${item.path}` })
				} else {
					// #ifdef APP-PLUS
					plus.runtime.openURL(encodeURI(item.path))
					// #endif
				}
			},
			escapePath(path) {
				return encodeURI(path)
			},
			cuibanShow(idx) {
				console.log(idx)
				if (!this.detail.auditors) return false
				// 第一个 还没审核 可以催
				if (idx == 0) {
					if (this.detail.auditors['auditor' + idx].sta == 0) {
						return true
					}
				} else {
					// 不是第一个
					// 上一个审核通过了
					if (this.detail.auditors['auditor' + (idx - 1)].sta == 1) {
						// 这个还没审核
						if (this.detail.auditors['auditor' + idx].sta == 0) {
							return true
						}
					}
				}
				return false
			},
			async cuiban(uid, sta = '待审批') {
				uni.showModal({
					title: "提示",
					content: "是否催办？",
					success: async (res) => {
						if (res.confirm) {
							await websock.cuiban({uid: uid, processName: '公文流转申请', processStatus: sta})
							uni.showToast({ title: '催办成功' })
						}
					}
				});
			}
		}
	}
</script>

<style>
	@import url("../common/detail.css");
</style>
